export const dateDiff = function (timestamp) {
    // 补全为13位
    let arrTimestamp = (timestamp + '').split('');
    for (let start = 0; start < 13; start++) {
        if (!arrTimestamp[start]) {
            arrTimestamp[start] = '0';
        }
    }
    timestamp = arrTimestamp.join('') * 1;

    let minute = 1000 * 60;
    let hour = minute * 60;
    let day = hour * 24;
    let halfamonth = day * 15;
    let month = day * 30;
    let now = new Date().getTime();
    let diffValue = now - timestamp;

    // 如果本地时间反而小于变量时间
    if (diffValue < 0) {
        return '不久前';
    }

    // 计算差异时间的量级
    let monthC = diffValue / month;
    let weekC = diffValue / (7 * day);
    let dayC = diffValue / day;
    let hourC = diffValue / hour;
    let minC = diffValue / minute;

    // 数值补0方法
    let zero = function (value) {
        if (value < 10) {
            return '0' + value;
        }
        return value;
    };

    // 使用
    if (monthC > 12) {
        // 超过1年，直接显示年月日
        return (function () {
            let date = new Date(timestamp);
            return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
        })();
    } else if (monthC >= 1) {
        return parseInt(monthC) + "月前";
    } else if (weekC >= 1) {
        return parseInt(weekC) + "周前";
    } else if (dayC >= 1) {
        return parseInt(dayC) + "天前";
    } else if (hourC >= 1) {
        return parseInt(hourC) + "小时前";
    } else if (minC >= 1) {
        return parseInt(minC) + "分钟前";
    }
    return '刚刚'
}


// 时间戳转日期格式
export const formatDate  = function(value) {
	let time = ''
	if(value) {
		let date = new Date();
		date.setTime(value);
		let month = date.getMonth() + 1;
		let hours = date.getHours();
		if (hours < 10)
				hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
				minutes = "0" + minutes;
		time = date.getFullYear() + "-" + month + "-" + date.getDate() +
				" " + hours + ":" + minutes;
		return time;
	}else{
		return time = "---";
	}
	
}
// 时间戳转日期格式--不带时间
export const formTime  = function(value) {
	let time = ''
	if(value) {
		let date = new Date();
		date.setTime(value);
		let month = date.getMonth() + 1;
		let hours = date.getHours();
		if (hours < 10)
				hours = "0" + hours;
		let minutes = date.getMinutes();
		if (minutes < 10)
				minutes = "0" + minutes;
		time = date.getFullYear() + "-" + month + "-" + date.getDate() 
			
		return time;
	}else{
		return time = "---";
	}
	
}
// 10位时间戳转日期与周几
export const getDateWeek = (time,status)=>{
        let time1 = time*1000;
        let time2 = new Date(time1);
        var   year=time2.getFullYear();   
        var   month=time2.getMonth()+1;  
        var   date=time2.getDate();  
        var   hour=time2.getHours();   
        var   minute=time2.getMinutes();    
        var   second=time2.getSeconds();
        let arys = year+"-"+month+"-"+date 
        let week = ''
        // let weekNum = new Date(arys).getDay()
         switch (new Date(arys).getDay()) {
          case 0:week="周天";break
          case 1:week="周一";break
          case 2:week="周二";break
          case 3:week="周三";break
          case 4:week="周四";break
          case 5:week="周五";break
          case 6:week="周六";break
         }
       let dateWeek= month+"."+date+  week
       if(status) {
         return year+"/"+month+ "/"+date+"  "+hour+":"+minute
       }else{
         return dateWeek
       }
}